package org.tnc.hr.webapp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.tnc.hr.model.WorkDays;
import org.tnc.hr.service.WorkDaysManager;
import org.tnc.hr.util.DateUtils;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.hualu.xmlCode.webapp.CdnBaseController;

public class WorkDaysController extends CdnBaseController {

    private WorkDaysManager workDaysManager;

    public void setWorkDaysManager(WorkDaysManager workDaysManager) {
        this.workDaysManager = workDaysManager;
    }

    /**
     * 入口
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value = "/hr/listWorkDays")
    public String generateWorkDays(HttpServletRequest request, HttpServletResponse response, String searchDate, Map model) {

        String message = "";
//        searchDate = (String) request.getAttribute("searchDate");

        // 封装后的带周末的对象集合
        List<WorkDays> weekL = new ArrayList<WorkDays>();
        ArrayList<String> arrDays = new ArrayList<String>();

        // 查询日期为空
        if (StringUtils.isBlank(searchDate)) {
            Date date = new Date();
            arrDays = DateUtils.getAllDay(new Date());

            // 先查出数据库中是否有当月记录
            List<WorkDays> l = workDaysManager.getWorkDaysByMonth(DateUtils.getStartDateByMonth(date),
                    DateUtils.getEndDateByMonth(date));

            // 如果没有查到数据，则生成当月数据
            if (l.size() <= 0 || l == null) {
                message = "当月个工作日期没有生成，请生成当月工作日期！";

                for (int i = 0; i < arrDays.size(); i++) {
                    WorkDays we = new WorkDays();
                    String[] x = arrDays.get(i).split("-");
                    if (DateUtils.getIsWeekend(Integer.valueOf(x[0]), Integer.valueOf(x[1]), Integer.valueOf(x[2]))) {
                        we.setWorkDay(DateUtils.convertDate(arrDays.get(i)));
                        we.setIsWeekend(1);
                    } else {
                        we.setWorkDay(DateUtils.convertDate(arrDays.get(i)));
                        we.setIsWeekend(0);
                    }
                    weekL.add(we);
                }
            } else {
                // 否则取出数据库中的数据
                message = "当月个工作日期已经生成！";
                weekL = l;

            }
        } else {
            // 查询日期不为空

            Date date = DateUtils.convertDate(searchDate);
            arrDays = DateUtils.getAllDay(new Date());

            // 先查出数据库中是否有当月记录
            List<WorkDays> l = workDaysManager.getWorkDaysByMonth(DateUtils.getStartDateByMonth(date),
                    DateUtils.getEndDateByMonth(date));

            // 如果没有查到数据，则生成当月数据
            if (l.size() <= 0 || l == null) {
                message = "当月个工作日期没有生成，请生成当月工作日期！";

                for (int i = 0; i < arrDays.size(); i++) {
                    WorkDays we = new WorkDays();
                    String[] x = arrDays.get(i).split("-");
                    if (DateUtils.getIsWeekend(Integer.valueOf(x[0]), Integer.valueOf(x[1]), Integer.valueOf(x[2]))) {
                        we.setWorkDay(DateUtils.convertDate(arrDays.get(i)));
                        we.setIsWeekend(1);
                    } else {
                        we.setWorkDay(DateUtils.convertDate(arrDays.get(i)));
                        we.setIsWeekend(0);
                    }
                    weekL.add(we);
                }
            } else {
                // 否则取出数据库中的数据
                message = "当月个工作日期已经生成！";
                weekL = l;
            }
        }
        
        this.saveMessage(request,getText(message, request.getLocale()));
        request.setAttribute("searchDate", searchDate);
        model.put("weekL", weekL);
        return "/hr/listWorkDays";
    }

    @RequestMapping(value = "/hr/addWorkDays")
    public String adWorkDays(HttpServletRequest request, HttpServletResponse response, @RequestParam String[] workDay) {

        ArrayList<String> arrDays = new ArrayList<String>();
        arrDays = DateUtils.getAllDay(DateUtils.convertDate(workDay[1]));

        ArrayList<WorkDays> workDaysL = new ArrayList<WorkDays>();
        
        for (int i = 0; i < arrDays.size(); i++) {
            WorkDays wd = new WorkDays();
            if (this.equalsList(arrDays.get(i), workDay)){
                // true work day
                wd.setWorkDay(DateUtils.convertDate(arrDays.get(i)));
                wd.setIsWeekend(0);
                
            } else {
                // false not workDay
                wd.setWorkDay(DateUtils.convertDate(arrDays.get(i)));
                wd.setIsWeekend(1);
            }
            workDaysL.add(wd);
        }
        
        this.workDaysManager.saveWorkDaysByList(workDaysL);
        String message = "工作日期保存成功!";
        this.saveMessage(request, getText(message, request.getLocale()));
        return "/hr/listWorkDays";
    }

    private boolean equalsList(String s, String[] l) {
        
        boolean boo = false;
        for (int i = 0; i < l.length; i++) {
            String ls = l[i];
            if (s.equals(ls)) {
                boo = true;
            } else {
                boo = false;
            }
        }
        return boo;
    }

}
